const sharp = require('sharp');
const fs = require('fs');
const path = require('path');
const sizeOf = require("image-size")
const fileUtil = require('./utils/file-util')

// 图片目录
const imagesDir = 'E:/temp/abs/images';
// 输出目录
const outputDir = 'E:/temp/abs/out';
fileUtil.clearDirectory(outputDir)

// 读取目录下所有文件的大小
fs.readdir(imagesDir, (err, files) => {
    if (err) throw err;
    files.forEach(file => {
        const filePath = path.join(imagesDir, file);
        const stats = fs.statSync(filePath);
        const fileSizeInBytes = stats["size"];
        const dimensions = sizeOf(filePath)
        console.log(file, fileSizeInBytes, dimensions, filePath)
        let width = dimensions.width;
        let rate = 0;
        if(width >= 2400) {
            rate = 0.4;
        } else if(width >= 1600){
            rate = 0.5;
        } else if(width>=1000){
            rate = 0.6;
        } else if(width>=700){
            rate = 0.65;
        }
        if(rate){
            console.log('rate', rate)
            width = Math.floor(width * rate)
            if(dimensions.type!='gif'){
                sharp(filePath)
                    .resize(width, null) // 设置压缩后的图片尺寸
                    .toFile(path.join(outputDir, file))
                    .then(() => {
                        console.log(`${file} was compressed.`);
                    })
                    .catch(err => {
                        console.error(`An error occurred while compressing ${file}: ${err}`);
                    });
            } else {
                let file2 = file.replace('.gif', '.jpg')
                sharp(filePath)
                    .toFormat('jpg')
                    .resize(width, null) // 设置压缩后的图片尺寸
                    .toFile(path.join(outputDir, file2))
                    .then(() => {
                        console.log(`${file2} was compressed.`);
                    })
                    .catch(err => {
                        console.error(`An error occurred while compressing ${file2}: ${err}`);
                    });
            }

        } else if(fileSizeInBytes > 50*1024){
            if(dimensions.type=='jpg'){
                sharp(filePath)
                    .jpeg({ quality: 30 }) // 设置压缩后的图片尺寸
                    .toFile(path.join(outputDir, file))
                    .then(() => {
                        console.log(`${file} was compressed.`);
                    })
                    .catch(err => {
                        console.error(`An error occurred while compressing ${file}: ${err}`);
                    });
            } else if(dimensions.type=='png'){
                sharp(filePath)
                    .png({ quality: 30 }) // 设置压缩后的图片尺寸
                    .toFile(path.join(outputDir, file))
                    .then(() => {
                        console.log(`${file} was compressed.`);
                    })
                    .catch(err => {
                        console.error(`An error occurred while compressing ${file}: ${err}`);
                    });
            } else if(dimensions.type=='gif') {
                file2 = file.replace('.gif', '.jpg')
                console.log('AAA', file2, filePath)
                sharp(filePath)
                    .toFormat('jpg')
                    .jpeg({ quality: 40 }) // 设置压缩后的图片尺寸
                    .toFile(path.join(outputDir, file2))
                    .then(() => {
                        console.log(`${file2} was compressed.`);
                    })
                    .catch(err => {
                        console.error(`An error occurred while compressing ${file2}: ${err}`);
                    });
            }

        }
    });
});
